﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using simpleHotel.Business;

namespace simpleHotel.Web
{
    public partial class HotelList : BasePage
    {
        int PAGESIZE = 3;
        string hotelname = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetHotelRange();

                if (Request.Params["hn"] != null)
                {
                    hotelname = Request.Params["hn"].ToString();
                }

                string standard = "0";
                if (Session["STANDARD"] != null)
                    if(Session["STANDARD"].ToString()!="")
                            standard = Session["STANDARD"] + "";

                GetHotelList(hotelname,Int32.Parse(Request.Params["prv"].ToString()), standard, "0");
            }
        }

        protected string DisplayPager(string currentpage)
        {
            string res = "";
            if (ViewState["CURRENTPAGE"] != null)
            {
                if (currentpage == ViewState["CURRENTPAGE"].ToString())
                {
                    res = "class=\"active\"";
                }
            }
            return res;
        }

        public int PageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                    return Convert.ToInt32(ViewState["PageNumber"]);
                else
                    return 0;
            }
            set
            {
                ViewState["PageNumber"] = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            rptPages.ItemCommand += new RepeaterCommandEventHandler(rptPages_ItemCommand);
        }

        void rptPages_ItemCommand(object source,RepeaterCommandEventArgs e)
        {
            ViewState["CURRENTPAGE"] = Convert.ToInt32(e.CommandArgument);

            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;

            string standard = "0";
            if (Session["STANDARD"] != null)
                if (Session["STANDARD"].ToString() != "")
                    standard = Session["STANDARD"] + "";

            GetHotelList(hotelname, Int32.Parse(Request.Params["prv"].ToString()), standard, "0");
        }

        

        private void GetHotelList(string hotelname,int provinceid,string sortrange, string sortprice)
        {
            HotelBLL obj = new HotelBLL();
            DataTable table = new DataTable();
            table = obj.HotelSearch(hotelname,provinceid,sortrange, sortprice);
            lblHotelCount.Text = table.Rows.Count.ToString();

            PagedDataSource pgitems = new PagedDataSource();
            DataView dv = new DataView(table);
            pgitems.DataSource = dv;
            pgitems.AllowPaging = true;
            pgitems.PageSize = PAGESIZE;
            pgitems.CurrentPageIndex = PageNumber;
            ViewState.Add("LastPage", pgitems.PageCount - 1);
            if (pgitems.PageCount > 1)
            {
                rptPages.Visible = true;
                ArrayList pages = new ArrayList();
                for (int i = 0; i < pgitems.PageCount; i++)
                    pages.Add((i + 1).ToString());
                rptPages.DataSource = pages;
                rptPages.DataBind();
            }
            
            rpHotel.DataSource = pgitems;
            rpHotel.DataBind();
        }

        private void GetHotelRange()
        {
            HotelBLL obj = new HotelBLL();
            DataTable tb = new DataTable();
            tb = obj.HotelRange();
            ddlRate.DataSource = tb;
            ddlRate.DataTextField = "Description";
            ddlRate.DataValueField = "ID";
            ddlRate.DataBind();
            ddlRate.Items.Insert(0, new ListItem("Sắp xếp", "0", true));
        }

        protected void ddlPrice_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetHotelList(hotelname,Int32.Parse(Request.Params["prv"].ToString()), ddlRate.SelectedValue, ddlPrice.SelectedValue);
        }

        protected void ddlRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            PageNumber = 0;
            GetHotelList(hotelname,Int32.Parse(Request.Params["prv"].ToString()), ddlRate.SelectedValue, ddlPrice.SelectedValue);
        }
    }
}